/*
 * 作者：lovefc
 * 博客：http://lovefc.cn
 * 扣扣：1102952084 
 */
 
$(document).ready(function() {
    var accordion_head = $('.accordion > li > a'),

    accordion_body = $('.accordion li > .sub-menu');

    accordion_head.first().addClass('active').next().slideDown('normal');

    accordion_head.on('click',
    function(event) {
        event.preventDefault();

        if ($(this).attr('class') != 'active') {

            accordion_body.slideUp('normal');

            $(this).next().stop(true, true).slideToggle('normal');

            accordion_head.removeClass('active');

            $(this).addClass('active');
        }
    });

});

function openurl(url){
    chrome.tabs.create({url: url});
    return true;	
}

function href(name){
	var url = 'http://lovefc.cn';
	switch(name){
		case 'zhihu':
		   url = 'https://www.zhihu.com';
		   openurl(url);
		break; 
		/*
		case 'weibo':
		   url = 'https://www.weibo.com';
		   openurl(url);
		break;
		*/
		case 'gitee':
		   url = 'https://www.gitee.com';
		   openurl(url);
		break;
		case 'hupu':
		   url = 'https://www.hupu.com';
		   openurl(url);
		break;		
		case 'v2ex':
		   url = 'https://www.v2ex.com';
		   openurl(url);
		break;  
        case 'bili':
            url = 'https://www.bilibili.com/index.html';
			openurl(url);
        break;
        case 'hu60':
            url = 'https://hu60.cn/q.php/index.index.html';
			openurl(url);
        break;
        case 'kanxue':
            url = 'https://bbs.pediy.com';
			openurl(url);
        break;
        case 'tieba':
            url = 'https://tieba.baidu.com';
			openurl(url);
        break;			
	}
}


var bg = chrome.extension.getBackgroundPage();
var counts = bg.getCounts();
var zhihu_count = bg.getZhihuCount();
var weibo_count = bg.getWeiboCount();
var v2ex_count = bg.getV2exCount();
var gitee_count = bg.getGiteeCount();
var hupu_count = bg.getHupuCount();
var bili_count = bg.getBiliCount();
var hu60_count = bg.getHu60Count();
var kanxue_count = bg.getKanxueCount();
var tieba_count = bg.getTiebaCount();
$('#counts').text(counts);

var arr = {
	"zhihu":zhihu_count,
	//"weibo":weibo_count,
	"v2ex":v2ex_count,
	"gitee":gitee_count,
	"hupu":hupu_count,	
	"bili":bili_count,
	"hu60":hu60_count,
	"kanxue":kanxue_count,
	"tieba":tieba_count
};
$(".sub-menu li").hide();
chrome.storage.sync.get(null,function(items){
    for(x in arr){
        if(x in items){
           $('#'+x).text(arr[x]);
		   $('.'+x).show();
	    }
    }
});

$(".sub-menu li").on('click',function () {
	var class_name = $(this).attr('class');
	href(class_name);
});

$('#open_set').click(e => {
	window.open(chrome.extension.getURL('options.html'));
});



